package com.jerry.cui.dingtalk.addata;

import java.util.Hashtable;
import java.util.List;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;

public class AdUserService {
	public static LdapContext getContext(Hashtable hashtable) throws NamingException {
	    LdapContext ctx = new InitialLdapContext(hashtable, null);
	    return ctx;
	}
	
	 public static Hashtable env = new Hashtable();
	 static{
	     String adminName = "ldaptest";//username
	     String adminPassword = "Career@123";//password
	     String ldapURL = "LDAP://192.168.12.10:389";//ip:port
	     env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
	     env.put(Context.SECURITY_AUTHENTICATION, "simple");//LDAP访问安全级别："none","simple","strong"
	     env.put(Context.SECURITY_PRINCIPAL, adminName);// AD User
	     env.put(Context.SECURITY_CREDENTIALS, adminPassword);// AD Password
	     env.put(Context.PROVIDER_URL, ldapURL);// LDAP工厂类
	     env.put("com.sun.jndi.ldap.connect.timeout", "10000");//连接超时设置为10秒
	 }
	 
	public static void main(String[] args) throws NamingException {
    	String searchFilter = "(&(cn>='')(objectclass=person))";
        String searchBase = "OU=Career,DC=careerintlinc,DC=local";
		List<AdUser> rootNamingList = getAdUser(getContext(env),searchFilter,searchBase,SearchControls.SUBTREE_SCOPE);
		System.out.println(JSON.toJSONString(rootNamingList));
	}
	// 获取用户列表
    public static List<AdUser> getAdUser(LdapContext ctx, String searchFilter, String searchBase, int scope) throws NamingException {
        NamingEnumeration<?> answer = getSearchResult(ctx, searchFilter, searchBase, scope);
        List<AdUser> namingList = Lists.newArrayList();
        while (answer.hasMoreElements()) {
            SearchResult sr = (SearchResult)answer.next();
            Attributes attrs = sr.getAttributes();
            AdUser adUser = new AdUser();
            adUser.setName(attrs.get("name").toString());
            namingList.add(adUser);
        }
        return namingList;
    }

	public static NamingEnumeration getSearchResult(LdapContext ctx,String searchFilter,String searchBase, int scope) throws NamingException {
        SearchControls searchCtls = new SearchControls();
        String returnedAtts[] = {"name", "sAMAccountName","title", "mobile","department","distinguishedName",
        		"departmentNumber","id",
                "uSNChanged","uSNCreated","whenChanged","whenCreated"}; // 定制返回属性
        searchCtls.setReturningAttributes(returnedAtts);
        searchCtls.setReturningObjFlag(true);
		searchCtls.setSearchScope(scope);
        NamingEnumeration answer = ctx.search(searchBase, searchFilter, searchCtls);
        return answer;
    }
}
